<template>
  <div class="user-container">
    <!-- <van-nav-bar v-if="activeTab != 0" :title="title" :left-text="pathname == '/user'? '': '返回'" :left-arrow="pathname != '/user'" @click-left="onClickBack"/> -->
    <div class="user-content">
      <!-- <shoporder v-if="activeTab == 0"></shoporder>
      <shopgoods v-if="activeTab == 1"></shopgoods>
      <shopcenter v-if="activeTab == 2"></shopcenter>-->
      <home v-if="activeTab == 0"></home>
      <rideorder v-if="activeTab == 1"></rideorder>
    </div>
    <van-tabbar @change="change" v-model="activeTab" active-color="#ff9c15">
      <van-tabbar-item>
        <span :class="activeTab == 0? 'red' :'grey'">首页</span>
        <img
          slot="icon"
          :src="activeTab == 0 ? require('@/assets/img/btn_sy_Y.png') : require('@/assets/img/btn_sy_N.png')"
        >
      </van-tabbar-item>
      <van-tabbar-item>
        <span :class="activeTab == 1?'red':'grey'">订单</span>
        <img
          slot="icon"
          :src="activeTab == 1 ? require('@/assets/img/btn_dd_Y.png') : require('@/assets/img/btn_dd_N.png')"
        >
      </van-tabbar-item>
    </van-tabbar>
  </div>
</template>
<script>
import Home from "./home.vue";
import Rideorder from "./rideorder.vue";
// import Shoporder from "./shoporder.vue";
export default {
  components: {
    Home,
    Rideorder
  },
  data() {
    return {
      timer: null,
      websock: null,
      pathname: window.location.pathname,
      title: "首页",
      activeTab: 0,
      icon: {
        normal: "@/assets/img/btn_sy_Y.png",
        active: "@/assets/img/btn_sy_Y.png"
      }
    };
  },
  created() {
    let that = this;
    that.$toast.loading({
      message: "加载中..."
    });
    var Sukdata = {
      userId: localStorage.getItem("userid")
    };
    // console.log(Sukdata);
    that.axios
      .post(that.baseUrl + "/user/updateRiding", that.Qs.stringify(Sukdata), {
        headers: {
          "Content-Type": "application/x-www-form-urlencoded"
        }
      })
      .then(function(res) {
        if (res.request.status === 200) {
          if (res.data.meta.code == 200) {
            console.log(res.data);
            that.$toast.clear();
          } else if (res.data.meta.code == 501) {
          }
        }
      });
    // that.initWebSocket();
  },
  mounted() {
    this.android_location();
    var wsLogin = "ws://47.104.244.213:80/paotui/echoFriends";
    var ws = new WebSocket(wsLogin);
    ws.onopen = function() {
      // Web Socket 已连接上，使用 send() 方法发送数据
      var msg = '{"type":"text"}';
      var obj = {
        sendlon: localStorage.getItem("long"),
        sendlat: localStorage.getItem("lat"),
        sendid: localStorage.getItem("userid")
      };
      ws.send(JSON.stringify(obj));
      this.timer = setInterval(() => {
        ws.send(JSON.stringify(obj));
      }, 30000);
    };
    ws.onmessage = function(message) {
      // 消息监听
      showMessage(message.data);
    };
  },
  methods: {
    android_location() {
      let that = this;
      dsBridge.call("getLocation");
      dsBridge.register("setLocation", function(data) {
        // alert(JSON.parse(data).City);
        let str = JSON.parse(data);
        if (str.longitude == "") {
          that.$toast({
            icon: "none",
            message: "请打开您的GPS"
          });
        } else {
          localStorage.setItem("long", str.longitude);
          localStorage.setItem("lat", str.latitude);
        }
      });
    },
    change() {
      if (this.activeTab == 0) {
        this.title = "首页";
      } else if (this.activeTab == 1) {
        this.title = "订单";
      }
    },
    onClickBack() {},
    initWebSocket() {
      //初始化weosocket
//    const wsuri = "ws://wqnm.free.idcfengye.com/paotui/echoFriends";
			const wsuri = "ws://47.104.244.213:80/paotui/echoFriends";
      this.websock = new WebSocket(wsuri);
      this.websock.onmessage = this.websocketonmessage();
      this.websock.onopen = this.websocketonopen();
      this.websock.onerror = this.websocketonerror();
      this.websock.onclose = this.websocketclose();
    },
    websocketonopen() {
      //连接建立之后执行send方法发送数据
      let actions = { test: "12345" };
      this.websocketsend(JSON.stringify(actions));
    },
    websocketonerror() {
      //连接建立失败重连
      this.initWebSocket();
    },
    websocketonmessage(e) {
      //数据接收
      const redata = JSON.parse(e.data);
    },
    websocketsend(Data) {
      //数据发送
      this.websock.send(Data);
    },
    websocketclose(e) {
      //关闭
      console.log("断开连接", e);
    }
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (vm.activeTab == 0 && to.path == "/user") {
        vm.title = "首页";
      } else {
        vm.title = to.meta.title;
      }
      if (!localStorage.getItem("userid")) {
        vm.$router.push({
          name: "login"
        });
      }
    });
  }
};
</script>
<style scoped>
.user-content {
  width: 100%;
  font-size: 0.3rem;
}
.user-container {
  width: 100%;
}
.grey {
  color: #8a8a8a;
}
.red {
  color: #ff2f4e;
}
</style>
